package com.qf.cqut.controller;

import com.qf.cqut.Service.FoodService;
import com.qf.cqut.model.pojo.AFood;
import com.qf.cqut.result.ResponseCode;
import com.qf.cqut.result.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("foods")
@CrossOrigin("*")
public class FoodController {
    @Resource
    private FoodService foodService;
    @RequestMapping("list")
    @ResponseBody
    public ResponseData<List<AFood>> queryAll(Integer page,Integer limit){

        List<AFood> aFoodList = foodService.selectByLimit(page,limit);
        Integer count = foodService.count();
        if(aFoodList.isEmpty()){
            return new ResponseData<>(ResponseCode.ERROR);
        }
        return new ResponseData<>(ResponseCode.SUCCESS,count,aFoodList);
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseData delById(String[] ids){
        Integer i = foodService.delByIds(ids);
        if(i == 0) {
            return new ResponseData(ResponseCode.ERROR);
        }else{
            return new ResponseData(ResponseCode.SUCCESS);
        }
    }
    @RequestMapping("conditions")
    @ResponseBody
    public ResponseData<List<AFood>> queryByConditions(AFood aFood){
        List<AFood> aFoodList = foodService.queryByConditions(aFood);
        return new ResponseData<>(ResponseCode.SUCCESS,aFoodList);

    }
    @RequestMapping("add")
    @ResponseBody
    public ResponseData addAfood(AFood aFood){
        aFood.setCreateTime(LocalDateTime.now());
        Integer i = foodService.addAfood(aFood);

        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }
    @RequestMapping("edit")
    @ResponseBody
    public ResponseData editAfood(AFood aFood){
        aFood.setUpdateTime(LocalDateTime.now());
        Integer i = foodService.editAfood(aFood);
        if(i == 0){
            return new ResponseData<>(ResponseCode.ERROR);
        }else{
            return new ResponseData<>(ResponseCode.SUCCESS);
        }
    }


}
